package com.epower.web.action.fileupload;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.epower.base.BaseAction;

@Scope("prototype")
@Component()
public class FileUpload extends BaseAction {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private File imgFile;
    private String imgFileFileName;
    private UploadSuccessResult successResult = new UploadSuccessResult();
    private UploadFailResult failReuslt = new UploadFailResult();
    
    public String upload() {
        try {
            if(imgFile!=null){
                String filename = String.valueOf(new Date().getTime())+imgFileFileName;
                String url = "http://localhost:8080/epower-webapp-1.0.0/uploadimages/"+filename;
                File dest = new File(ServletActionContext.getServletContext().getRealPath("/uploadimages" ) + "/" +filename);
                FileUtils.copyFile(imgFile, dest);
                successResult.setError(0);
                successResult.setUrl(url);
                return SUCCESS;
            }else{
                failReuslt.setError(1);
                failReuslt.setMessage("imgFile is null");
                return ERROR;
            }
        } catch (IOException e) {
            failReuslt.setError(1);
            failReuslt.setMessage("upload fail");
            return ERROR;
        }
    }
    
    public File getImgFile() {
        return imgFile;
    }
    
    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }
    
    public UploadSuccessResult getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(UploadSuccessResult successResult) {
        this.successResult = successResult;
    }
    
    public UploadFailResult getFailReuslt() {
        return failReuslt;
    }
    
    public void setFailReuslt(UploadFailResult failReuslt) {
        this.failReuslt = failReuslt;
    }
    
    public String getImgFileFileName() {
        return imgFileFileName;
    }
    
    public void setImgFileFileName(String imgFileFileName) {
        this.imgFileFileName = imgFileFileName;
    }

    public class UploadSuccessResult{
        private int error;
        private String url;
        
        public int getError() {
            return error;
        }
        
        public void setError(int error) {
            this.error = error;
        }
        
        public String getUrl() {
            return url;
        }
        
        public void setUrl(String url) {
            this.url = url;
        }
        
        
    }
    
    public class UploadFailResult{
        private int error;
        private String message;
        
        public int getError() {
            return error;
        }
        
        public void setError(int error) {
            this.error = error;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
        
    }
}
